package com.tz.crm.controller.customer;

import com.tz.crm.dao.AddressDao;
import com.tz.crm.entity.*;
import com.tz.crm.entity.enumeration.Level;
import com.tz.crm.entity.enumeration.Status;
import com.tz.crm.service.*;
import com.tz.crm.utils.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by 何磊 on 2017/3/13.
 */
@Controller
@RequestMapping("/customer")
public class CustomerSearchSea {

    @Resource
    private ContactService contactService;
    @Resource
    private CustomerService customerService;
    @Resource
    private AddressDao addressDao;
    @Resource
    private VisitRecordService visitRecordService;
    @Resource
    private UserService userService;
    @Resource
    private  VisitTypeService visitTypeService;

    @RequestMapping("/getAllSea.do")
    public String backSea(){


        return "/backsea";
    }

    @RequestMapping("/searchSea.action")
    public String searchSea(Customer customer,Integer pageNumber, Model model){
        Pager<Customer> pageCus=new Pager<>();
        if(pageNumber==null){
            pageNumber=1;
        }
        pageCus.setPageNumber(pageNumber);
        //从后台获取customer 的数据
         pageCus= customerService.getByPager(customer,pageCus);
        //第一次进入 没有传pageNumber

        model.addAttribute("pageCus",pageCus);
        //在把form表单提交的customer对象放入到作用域中,方便分页的操作
        model.addAttribute("customer",customer);
        return "/backsea";
    }

    /**
     * 删除客户
     */
    @RequestMapping("/delete.action")
    public String delCustomer(@RequestParam String customers){
        //通过 ，截取 返回一个String数组
            String ids[]=customers.split(",");
            //调用方法删除客户的联系人
        for (String id : ids) {
            //删除客户的联系人、
            List<Contact> contacts=contactService.getByCustomerId(id);
            for (Contact contact : contacts) {
                contactService.delete(contact);
            }
            //删除地址、
            List<Address> addresses=addressDao.getAddressByCustomerId(id);
            for (Address address : addresses) {
                addressDao.delete(address);
            }

            //删除拜访记录
          visitRecordService.delVisitRecordByCusId(id);
        }
        //调用方法删除客户的地址
        customerService.delete(ids);
        return "/backsea";
    }

    /**
     * 查看联系人
     */
    @RequestMapping("/contactList.action")
    public String contractList(@RequestParam String id,Model model){
    //根据客户id 获取所有联系人
    List<Contact> contacts=contactService.getByCustomerId(id);
        //放入作用域中
        model.addAttribute("contacts",contacts);
        return "contractList";
    }

    /**
     * 拜访记录
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/visitRecordList.action")
    public String visitRecordList(@RequestParam String id,Integer pageNumber,Model model){
    //根据客户id 获取其所有拜访记录
        Pager<VisitRecord> pager=new Pager<>();
        //第一次进入 没有传pageNumber
        if(pageNumber==null){
            pageNumber=1;
        }
        pager.setPageNumber(pageNumber);
    pager=visitRecordService.getByPager(id,pager);
    model.addAttribute("pagerRe",pager);
    //将id 放入作用域中 方便分页查
    model.addAttribute("customerId",id);
        //放入作用域中
        return "visitRecord";
    }

    /**
     * 捡选客户
     */
    @RequestMapping("/pick.action")
    public String pickCustomer(@RequestParam String customers, HttpSession session,Model model){
        //通过 ，截取 返回一个String数组
        String ids[]=customers.split(",");
        //获取session的用户
        User user= (User) session.getAttribute("user");
        //获取选取的客户集合
        List<Customer> customerList=customerService.get(ids);
        //给每个客户user属性赋值（及给用户添加客户）
        for (Customer customer : customerList) {
            customer.setOwner(user);

            //把每个公海客户的归还联系人id设为空
            customer.setBackSeaUserId(null);
            //更新客户
            customerService.update(customer);
        }
        return "backsea";
    }

    /**
     * 分配客户
     */

    @RequestMapping("/initDispatch.action")
    public String dispatchCus( @RequestParam String customers,Model model){
        //获取user集合
        List<User> users=userService.getAll();
        //放入作用域中
        model.addAttribute("users",users);
        model.addAttribute("customersId",customers);
        return "dispatch";
    }

    /**
     * 确定分配
     */
    @RequestMapping("/dispatch.do")
    public String confimDispatch( @RequestParam String customers,String id, Model model){
        //根据User id 查找到user
        User user=userService.get(id);
        String ids[]=customers.split(",");
        List<Customer> customerList=customerService.get(ids);
        //给每个客户user属性赋值（及给用户添加客户）
        for (Customer customer : customerList) {
            customer.setOwner(user);
            //把每个公海客户的归还联系人id设为空
            customer.setBackSeaUserId(null);
            //更新客户
            customerService.update(customer);
        }
        return "backsea";
    }
    /**
     * 新增拜访记录页面
     */
    @RequestMapping("/initRecord.do")
    public String addVisitRecord(@RequestParam String id, Model model){
        List<Contact> contacts=contactService.getByCustomerId(id);

        //获取所有的拜访方式
        List<VisitType> visitTypes=visitTypeService.getAll();
        model.addAttribute("contacts",contacts);
        model.addAttribute("visitTypes",visitTypes);
        model.addAttribute("customerId",id);
        //跳转到拜访记录
        return "addVisitRecord";
    }

    /**
     * 确认添加拜访记录
     */
    @RequestMapping("/saveVisit.do")
    public String saveVisitRecord(String  visitType, Level level, HttpSession session, Status status, String content, String customerId,Model model){
        //从作用域中获取user
        User user= (User) session.getAttribute("user");
        VisitType vt=visitTypeService.get(visitType);
        Customer customer=customerService.get(customerId);

        VisitRecord visitRecord=new VisitRecord();
        //設置屬性
        visitRecord.setCustomer(customer);
        visitRecord.setVisitType(vt);
        visitRecord.setLevel(level);
        visitRecord.setOwner(user);
        visitRecord.setStatus(status);
        visitRecord.setContent(content);
        visitRecord.setVisitDate(new Date());
        System.out.println("visitRecord = " + visitRecord);
        //保存一条访问记录
        visitRecordService.save(visitRecord);
        //跳转到拜访记录
        return "backsea";
    }

}
